<template>
  <div>
    <el-dialog :title="meta" :visible="showDialog" @close="closeForm">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="学科编号" prop="rid">
          <el-input v-model.trim="form.rid" />
        </el-form-item>
        <el-form-item label="学科名称" prop="name">
          <el-input v-model.trim="form.name" />
        </el-form-item>
        <el-form-item label="学科简称">
          <el-input v-model.trim="form.short_name" />
        </el-form-item>
        <el-form-item label="学科简介">
          <el-input v-model.trim="form.intro" />
        </el-form-item>
        <el-form-item label="学科备注">
          <el-input v-model="form.remark" />
        </el-form-item>
        <el-form-item style="display: flex; justify-content: center">
          <el-button @click="closeForm">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getSubList, addSub, editSub } from '../../../api/subject'
export default {
  data() {
    var testrid = async(rule, value, callback) => {
      if (this.meta === '新增学科') {
        const res = await getSubList()
        if (res.data.items.find(v => v.rid === value)) {
          callback(new Error('学科编号重复'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    var testName = async(rule, value, callback) => {
      if (this.meta === '新增学科') {
        const res = await getSubList()
        if (res.data.items.find(v => v.name === value)) {
          callback(new Error('学科名称重复'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }

    return {
      meta: '新增学科',
      showDialog: false,
      form: {},
      formLabelWidth: '120px',
      rules: {
        rid: [
          { required: true, message: '请输入学科编号', trigger: 'blur' },
          { validator: testrid, message: '学科编号重复', trigger: 'blur' }
        ],
        name: [{ required: true, message: '请输入学科名称', trigger: 'blur' },
          { validator: testName, message: '学科名称重复', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.meta === '新增学科') {
            await addSub(this.form)
            this.$message.success('新增学科成功')
          } else {
            const res = await editSub(this.form)
            console.log(res)
            this.$message.success('编辑学科成功')
          }
          this.closeForm()
          this.$emit('getSubList')
        } else {
          this.message.success('校验不通过')
          return false
        }
      })
    },
    closeForm() {
      this.form = {}
      this.showDialog = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

    <style scoped lang="scss">
    ::v-deep {
      .el-dialog__header {
        background-color: #07b3fa;
        text-align: center;
      }
      .el-dialog__title,
      .el-dialog__close {
        color: #fff;
      }
      .el-dialog__close:hover {
        color: #fff;
      }
    }
    </style>

